package tr.edu.yildiz.dap.appeditor.pages.composites;

import java.util.List;

import org.eclipse.jface.viewers.ILabelProvider;
import org.eclipse.jface.viewers.IOpenListener;
import org.eclipse.jface.viewers.ISelectionChangedListener;
import org.eclipse.jface.viewers.SelectionChangedEvent;
import org.eclipse.jface.viewers.StructuredSelection;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ControlAdapter;
import org.eclipse.swt.events.ControlEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.ui.forms.widgets.FormToolkit;

import tr.edu.yildiz.ce.dap.appagent.model.Component;
import tr.edu.yildiz.dap.appeditor.model.ListUnit;

public class ListEditorComposite extends Composite{

	TableViewer viewer;
	
	Button addButton;
	
	Button removeButton;
	
	boolean readOnly = false;
	
	public ListEditorComposite(Composite parent, int style) {
		super(parent, style);
		FormToolkit toolkit = new FormToolkit(parent.getDisplay());
		 
		GridLayout gridLayout = new GridLayout(2, false);
	    gridLayout.marginWidth = 1;
	    gridLayout.marginHeight = 1;
	    gridLayout.verticalSpacing = 1;
	    setLayout(gridLayout);

	    final Table table = toolkit.createTable(this, SWT.FLAT | SWT.MULTI | style);
	    table.setData("name", "list-editor-composite-table");
	    final TableColumn column = new TableColumn(table, SWT.NONE);
	    table.addControlListener(new ControlAdapter() {
	      public void controlResized(ControlEvent e) {
	        column.setWidth(table.getClientArea().width);
	      }
	    });
	    
	    viewer = new TableViewer(table);

	    int vSpan = 3;
	    GridData viewerData = new GridData( SWT.FILL, SWT.FILL, true, true, 1, vSpan);
	    viewerData.widthHint = 100;
	    viewerData.heightHint = 125;
	    viewerData.minimumHeight =125;
	    table.setLayoutData(viewerData);
	    viewer.setData(FormToolkit.KEY_DRAW_BORDER, Boolean.TRUE);
	    GridData gd = null;
	    
	    addButton = toolkit.createButton(this, "Add", SWT.FLAT);
	    gd = new GridData(SWT.FILL, SWT.TOP, false, false);
	    gd.verticalIndent=0;
	    addButton.setLayoutData(gd);
	    addButton.setEnabled(true);

	    removeButton = toolkit.createButton(this, "Delete", SWT.FLAT);
	    gd = new GridData(SWT.FILL, SWT.TOP, false, false);
	    gd.verticalIndent=0;
	    removeButton.setLayoutData(gd);
	    removeButton.setEnabled(false);

	    viewer.addSelectionChangedListener(new ISelectionChangedListener() {
	      public void selectionChanged(SelectionChangedEvent event) {
	    //    updateRemoveButton();
	      }
	    });
	    
	    toolkit.paintBordersFor(this);
	}
	
	public void setLabelProvider(ILabelProvider labelProvider) {
		viewer.setLabelProvider(labelProvider);
	}

	public void setContentProvider(ListEditorContentProvider<Component> contentProvider) {
		viewer.setContentProvider(contentProvider);
	}

	public void setInput(List<Component> input) {
		viewer.setInput(input);
		viewer.setSelection(new StructuredSelection());
	}
	
	public List<Component> getInput(){
		return (List<Component>) viewer.getInput();
	}

	public void setOpenListener(IOpenListener listener) {
		viewer.addOpenListener(listener);
	}

	public void addSelectionListener(ISelectionChangedListener listener) {
		viewer.addSelectionChangedListener(listener);
	}
	

	public void setAddListener(SelectionListener listener) {
		addButton.addSelectionListener(listener);
		addButton.setEnabled(true);
	}

	public void setRemoveListener(SelectionListener listener) {
		removeButton.addSelectionListener(listener);
	}

	public void setReadOnly(boolean readOnly) {
		this.readOnly = readOnly;
	}
	
	private boolean isReadOnly() {
		return readOnly;

	}
	
	public void refresh() {
		viewer.refresh();
	}
	
	public Component getSelected(){
		return (Component)((StructuredSelection) viewer.getSelection()).getFirstElement();
	}
	
	public Button getRemoveButton() {
		return removeButton;
	}
	
	public Button getAddButton() {
		return addButton;
	}
	
}
